﻿using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
using WindowServerManager.Model;
using WindowServerManager.Util;
using static WindowServerManager.Util.Enums;

namespace WindowServerManager
{
    public partial class FrmMain : Form
    {
        private Dictionary<string, string> statusDic = new Dictionary<string, string>();
        public FrmMain()
        {
            InitializeComponent();

        }

        /// <summary>
        /// 加载服务
        /// </summary>
        /// <param name="serverName"></param>
        public void InitServer(string serverName = "")
        {
            List<Server> list = new List<Server>();
            //获得服务集合
            ServiceController[] serviceControllers = ServiceController.GetServices();
            //遍历服务集合，打印服务名和服务状态
            foreach (ServiceController service in serviceControllers)
            {
                var NameUpper=service.ServiceName.ToUpper();
                if (string.IsNullOrEmpty(serverName))
                {
                    Server model = new Server();
                    model.Name = service.ServiceName;
                    model.Description = service.DisplayName;
                    model.Status = statusDic[((int)service.Status).ToString()];
                    list.Add(model);
                }
                else
                {
                    if (NameUpper.Contains(serverName.ToUpper()))
                    {
                        Server model = new Server();
                        model.Name = service.ServiceName;
                        model.Description = service.DisplayName;
                        model.Status = statusDic[((int)service.Status).ToString()];
                        list.Add(model);
                    }
                }
            }
            this.dgvServerList.DataSource = list;
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Server server = GetSelectRow();
            if (server != null)
            {
                WindowServerHelp.RunService(server.Name);
            }
            else
            {
                MessageBox.Show("没有选中行");
            }
            InitServer();
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Server server = GetSelectRow();
            if (server != null)
            {
                WindowServerHelp.StopService(server.Name);
            }
            else
            {
                MessageBox.Show("没有选中行");
            }
            InitServer();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            statusDic = EnumExtension.GetEnums<Status>();
            InitServer();
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbQuery_TextChanged(object sender, EventArgs e)
        {
            InitServer(this.tbQuery.Text);
        }

        /// <summary>
        /// 获取选中行
        /// </summary>
        /// <returns></returns>
        public Server GetSelectRow()
        {
            Server server = null;
            if (dgvServerList.SelectedRows != null && dgvServerList.SelectedRows.Count > 0)
            {
                server = dgvServerList.SelectedRows[0].DataBoundItem as Server;
            }
            return server;
        }

        /// <summary>
        /// 恢复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            Server server = GetSelectRow();
            if (server != null)
            {
                var serverController = WindowServerHelp.GetServerController(server.Name);
                serverController.Continue();
            }
            else
            {
                MessageBox.Show("没有选中行");
            }
            InitServer();
        }

        /// <summary>
        /// 创建服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FrmWindowServerPath dialog = new FrmWindowServerPath("创建服务");
            dialog.ShowDialog();
            WindowServerHelp.InstallService(dialog.Path);
            dialog.Close();
            MessageBox.Show("创建服务成功");
            InitServer();
        }

        /// <summary>
        /// 暂停服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Server server = GetSelectRow();
            if (server != null)
            {
                var serverController = WindowServerHelp.GetServerController(server.Name);
                serverController.Pause();
            }
            else
            {
                MessageBox.Show("没有选中行");
            }
            InitServer();
        }

        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            Server server = GetSelectRow();
            if (server != null)
            {
                FrmWindowServerPath dialog = new FrmWindowServerPath("卸载服务");
                dialog.ShowDialog();
                WindowServerHelp.UnInstallService(dialog.Path);
                dialog.Close();
                MessageBox.Show("卸载服务成功");
                InitServer();
            }
            else
            {
                MessageBox.Show("没有选中行");
            }
        }
    }
}
